/*
 * Copyright (c) Created by T C L on 2019.7.5.
 */

package cn.mumzone.pos.presenter;

import cn.mumzone.basic.mvp.AbsPresenter;
import cn.mumzone.basic.net.http.callback.ApiCallBack;
import cn.mumzone.basic.net.response.BaseResponse;
import cn.mumzone.basic.net.response.PageListResponse;
import cn.mumzone.basic.util.RxBus;
import cn.mumzone.pos.event.SettleSuccessEvent;
import cn.mumzone.pos.event.TimesCardRefreshEvent;
import cn.mumzone.pos.model.TimeCardEntity;
import cn.mumzone.pos.model.source.MemberRepository;
import cn.mumzone.pos.presenter.contract.TimesCardContract;
import rx.Subscription;
import rx.functions.Action1;

public class TimesCardPresenter extends AbsPresenter<TimesCardContract.View, MemberRepository> implements TimesCardContract.Presenter {

    private String memberId;

    @Override
    public void getTimesCardAllListPage(String memberId, String viewType, String page, String pageSize) {
        this.memberId = memberId;
        ApiCallBack<PageListResponse<TimeCardEntity>> timesCardCallback =
                new ApiCallBack<PageListResponse<TimeCardEntity>>() {
                    @Override
                    public void onSuccess(PageListResponse<TimeCardEntity> timesCard) {
                        mView.setTimesCardData(timesCard);
                        mView.dismissLoading();
                    }

                    @Override
                    public void onError(BaseResponse response) {
                        mView.showError(response.getMsg());
                        mView.dismissLoading();
                    }

                    @Override
                    public void onNetworkError(BaseResponse response) {
                        mView.showNetError();
                    }
                };
        mRepo.getTimesCardAllListPage(memberId, viewType, page, pageSize, timesCardCallback);
        addSubscrebe(timesCardCallback);
    }

    @Override
    public void initEvent() {
        // 退卡刷新次卡 Event
        Subscription subscription = RxBus.getDefault()
                .toObservable(TimesCardRefreshEvent.class)
                .subscribe(new Action1<TimesCardRefreshEvent>() {
                    @Override
                    public void call(TimesCardRefreshEvent timesCardRefreshEvent) {
                        mView.refreshData();
                    }
                });
        addSubscrebe(subscription);


        //买卡刷新次卡
        Subscription purchaseCardSubscription = RxBus.getDefault()
                .toObservable(SettleSuccessEvent.class)
                .subscribe(new Action1<SettleSuccessEvent>() {
                    @Override
                    public void call(SettleSuccessEvent settleSuccessEvent) {
                        mView.refreshData();
                    }
                });
        addSubscrebe(purchaseCardSubscription);
    }
}
